<template>
    <div>
        <div class="tab-back"><span class="left" @click="toback()"></span><div class="title">更新料</div><div class="right"></div></div>
        <div class="box-r4" style="padding-bottom: 15px;">
            <div class="tips_title">（必填一项）</div>
            <textarea v-model="content" placeholder="更新内容" class="add-tarea-input" ></textarea>
            <van-uploader  v-model="fileList1" multiple :after-read="upimg1" style="margin-bottom: 20px" :before-delete="del">
                <img src="../../assets/img/addimg.png" class="addimg_icon">
            </van-uploader>
        </div>
        <input type="button" class="red_btn btn-bottom" value="确认更新" @click="changesub()">
    </div>
</template>

<script>
    export default {
        name: "",
        data(){
            return {
                content:"",
                fileList1:[],
                pics:[]
            }
        },
        mounted(){
            this.materialsid = this.$route.params.id;
        },
        methods:{
            //删除图片
            del(file){
                var that=this, delimgurl=file.content;

                for(var i=0; i<that.fileList1.length;i++){
                    if(that.fileList1[i].content==delimgurl){
                        that.pics.splice(i,1)
                    }
                }
                return file;
            },
            //提交更新内容
            changesub(){
                if(!this.content && this.pics.length<1){
                    this.$toast("未更新内容");
                    return false;
                }
                var that=this;

                that.request1.post1({
                    url: that.Url + "api/materials/editmateSave",
                    params: { materialsid:that.materialsid, content:that.content, pics:JSON.stringify(that.pics)},
                    success: function (res) {
                        console.log(res)
                        if (res.data.code == 1) {
                            that.$toast.success("更新成功");
                            that.$router.go(-1);
                        } else {
                            alert(res.data.msg)
                        }
                    },
                    fail: function (error) {
                        alert(error.data.msg)
                    }
                })
            },
            toback(){
                this.$router.go(-1);//返回上一层
            },
            upimg1(file){
                var that=this;

                if(file.constructor===Array){
                    for(var i=0; i<file.length; i++){
                        file[i].status = 'uploading';
                        file[i].message = '上传中';

                        let fd = new FormData();
                        fd.append('file', file[i].file);
                        that.saveImg(fd,file[i]);
                    }
                }else {
                    file.status = 'uploading';
                    file.message = '上传中';
                    let fd = new FormData();
                    fd.append('file', file.file);

                    that.saveImg(fd,file);
                }
            },
            saveImg(fd,file){
                var that=this;
                that.request1.post1({
                    url: that.Url + "api/common/upload",
                    params: fd,
                    success: function (res) {
                        console.log(res);
                        if (res.data.code == 1) {
                            that.pics.push(res.data.data.url);
                            file.status = 'done';
                            file.message = '上传成功';
                        } else {
                            file.status = 'failed';
                            file.message = '上传失败';
                            that.$toast(res.data.msg);
                        }
                    },
                    fail:function (error) {
                        file.status = 'failed';
                        file.message = '上传失败';
                        that.$toast(error.data.msg);
                    }
                })
            }
        }
    }
</script>

<style scoped>
    .tab-back {
        height: 44px;
        background: #fc5d4d;
        display: -webkit-box;
    }
    .tab-back .left {
        width: 44px;
        display: block;
        height: 44px;
        background: url() no-repeat 50%;
        background-size: 9px 16px;
    }
    .tab-back .title {
        -webkit-box-flex: 1;
        font-size: 15px;
        color: #fff;
        font-weight: 700;
        text-align: center;
        line-height: 44px;
    }
    .tab-back .right {
        width: 44px;
        height: 44px;
    }
    .box-r4, .red_btn {
        border-radius: 4px;
    }
    .box-r4 {
        display: block;
        background: #fff;
        margin: 7px 10px 0;
        padding: 0 15px;
        overflow: hidden;
    }
    .tips_title {
        font-size: 14px;
        font-weight: 700;
        color: #000;
        display: block;
        line-height: 14px;
        padding-top: 20px;
        padding-bottom: 10px;
    }
    .tips_title p {
        font-size: 12px;
        display: inline-block;
        padding-left: 10px;
    }
    .red_ft {
        color: #fc5d4d !important;
    }
    .input_text {
        height: 36px;
        border-radius: 4px;
        font-size: 13px;
        border: 1px solid #e5e5e5;
        padding: 0 4%;
        background: #f9f9fb;
        width: 92%;
        display: block;
        margin-bottom: 15px;
    }
    .red_btn {
        background: #fc5d4d;
        color: #fff;
        font-size: 14px;
        text-align: center;
        height: 40px;
        line-height: 40px;
        width: 90%;
        margin: 20px 5% 100px;
        box-shadow: 0 5px 10px #f6a59f;
    }
    .box-r4, .red_btn {
        border-radius: 4px;
    }
    .box-r4 .add-tarea-input {
        letter-spacing: .2px;
        display: block;
        padding: 10px 4%;
        width: 100%;
        background: #f9f9fb;
        min-height: 80px;
        line-height: 20px;
        font-size: 13px;
        border-radius: 4px;
        border: 1px solid #e5e5e5;
        margin-bottom: 16px;
        box-sizing: border-box;
    }
    .btn-bottom {
        position: fixed;
        bottom: 20px;
        margin-bottom: 0 !important;
    }
    .addimg_icon{
        width: 80px;
        height: 80px;
    }
</style>